package com.mjh.zhujianju.service.set.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.livew.framework.base.dao.SysUserMapper;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.enums.EStatus;
import com.livew.framework.enums.EUserRoleType;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.sql.XTSqlUtils;
import com.livew.framework.utils.XTListUtils;
import com.livew.framework.utils.XTStringUtils;
import com.mjh.zhujianju.model.set.PermissionEO;
import com.mjh.zhujianju.model.set.PermissionUserEO;
import com.mjh.zhujianju.service.agency.DeptUserService;
import com.mjh.zhujianju.service.set.AdminService;
import com.mjh.zhujianju.service.set.PermissionService;
import com.mjh.zhujianju.service.set.PermissionUserService;
import com.mjh.zhujianju.util.ShiroEncryption;
import com.mjh.zhujianju.vo.agency.DeptUserVO;
import com.mjh.zhujianju.vo.base.SysUserVO;

/**
 * @ClassName AdminServiceImpl
 * @author wt1293
 * @Date 2018年8月21日 下午2:00:46
 * @version V1.0
 * @Description
 */
@Service
public class AdminServiceImpl extends BaseServiceImpl<SysUser> implements AdminService {

	@Resource
	private SysUserMapper sysUserMapper;

	@Resource
	private SysUserService sysUserService;

	@Resource
	private FileService fileService;

	@Resource
	private DeptUserService deptUserService;

	@Resource
	private PermissionUserService permissionUserService;
	
	@Resource
	private PermissionService permissionService;

	/**
	 * @param baseMapper
	 */
	public AdminServiceImpl(SysUserMapper sysUserMapper) {
		super(sysUserMapper);
	}

	@Override
	@Transactional
	public void saveInfo(SysUserVO vo) throws XTBusinessException {
		SysUser eo = new SysUser();
		BeanUtils.copyProperties(vo, eo);
		eo.setStatus(EStatus.active.getCode());
		eo.setType(EUserRoleType.message.getCode());
		eo.setPassword(ShiroEncryption.encrptText(eo.getPassword()));
		this.save(eo);
		if (!XTListUtils.isEmpty(vo.getPermissionList())) {
			List<Long> ids = new ArrayList<>();
			for (PermissionUserEO p : vo.getPermissionList()) {
				if (p.getPermissionId() != null) {
					p.setUserId(eo.getId());
					this.permissionUserService.save(p);
					ids.add(p.getPermissionId());
				}
			}
			//一个大类中有一个子类被选择了，则也要添加一个大类的
			if(!XTListUtils.isEmpty(ids)) {//取父url的主键
				List<PermissionEO>  list= permissionService.getAllByIds(ids);
				if(!XTListUtils.isEmpty(list)) {
				Set<Long> set = new HashSet<>();	
				for (PermissionEO permissionEO : list) {
					if(permissionEO.getIdParent()!=0) {
						set.add(permissionEO.getIdParent());
					}
				}
				for (Long id : set) {
					PermissionUserEO p = new PermissionUserEO();
					p.setPermissionId(id);
					p.setUserId(eo.getId());
					this.permissionUserService.save(p);
				}
				}
			}
		}
	}

	@Override
	public void updateInfo(SysUserVO vo) throws XTBusinessException {
		SysUser eo = new SysUser();
		if(XTStringUtils.isEmpty(vo.getPassword())) {
			vo.setPassword(null);
		}
		BeanUtils.copyProperties(vo, eo);
		eo.setStatus(EStatus.active.getCode());
		this.updateByPrimaryKeySelective(eo);
		this.permissionUserService.deleteByUserId(eo.getId());
		if (!XTListUtils.isEmpty(vo.getPermissionList())) {
			List<Long> ids = new ArrayList<>();
			for (PermissionUserEO p : vo.getPermissionList()) {
				if (p.getPermissionId() != null) {
					p.setUserId(eo.getId());
					this.permissionUserService.save(p);
					ids.add(p.getPermissionId());
				}
			}
			//一个大类中有一个子类被选择了，则也要添加一个大类的
			if(!XTListUtils.isEmpty(ids)) {//取父url的主键
				List<PermissionEO>  list= permissionService.getAllByIds(ids);
				if(!XTListUtils.isEmpty(list)) {
				Set<Long> set = new HashSet<>();	
				for (PermissionEO permissionEO : list) {
					set.add(permissionEO.getIdParent());
				}
				for (Long id : set) {
					PermissionUserEO p = new PermissionUserEO();
					p.setPermissionId(id);
					p.setUserId(eo.getId());
					this.permissionUserService.save(p);
				}
				}
			}
		}
	}

	@Override
	public PagedResult<SysUserVO> getVOPage(SysUser vo) {
		Integer pageNo = vo.getPageNo() == null ? 1 : vo.getPageNo();
		Integer pageSize = vo.getPageSize() == null ? 15 : vo.getPageSize();
		PageHelper.startPage(pageNo, pageSize);
		if(!XTStringUtils.isEmpty(vo.getRemark2())) {
			vo.setRemark2(XTSqlUtils.replaceSeparator(vo.getRemark2()));
		}
		List<SysUser> list = sysUserMapper.selectPagination(vo);
		PagedResult<SysUser> page = PagedBeanUtil.toPagedResult(list);
		PagedResult<SysUserVO> result = new PagedResult<>();
		BeanUtils.copyProperties(page, result);
		if (!XTListUtils.isEmpty(list)) {
			List<SysUserVO> voList = new ArrayList<SysUserVO>();
			List<Long> userIds = new ArrayList<>();
			for (SysUser sysUser : list) {
				userIds.add(sysUser.getUserId());
				SysUserVO sysUserVO = new SysUserVO();
				BeanUtils.copyProperties(sysUser, sysUserVO);
				voList.add(sysUserVO);
			}
			// 获取用户基本信息
			Map<Long, SysUser> userInfoMap = sysUserService.getMapByIdList(userIds);
			Map<Long, DeptUserVO> depts = deptUserService.getDeptByUserIds(userIds);// 获取部门信息
			for (SysUserVO sysUserVO : voList) {
				if (userInfoMap != null) {
					SysUser user = userInfoMap.get(sysUserVO.getUserId());
					if (user != null) {
						sysUserVO.setNickname(user.getNickname());
					}
				}
				if (depts != null) {
					DeptUserVO dept = depts.get(sysUserVO.getUserId());
					sysUserVO.setDeptUser(dept);
				}
			}
			result.setDataList(voList);
		}
		return result;
	}

	@Override
	public SysUserVO getInfo(Long id) throws XTBusinessException {
		SysUser wd = super.getByPrimaryKey(id);
		if (wd != null) {
			SysUserVO vo = new SysUserVO();
			BeanUtils.copyProperties(wd, vo);
			if(vo.getUserId()!=null) {
			SysUser user =	sysUserMapper.selectByPrimaryKey(vo.getUserId());
			if(user!=null) {
				vo.setNickname(user.getNickname());
			}
			DeptUserVO dept =	deptUserService.getDeptByUserId(wd.getUserId());
			if(dept!=null) {
				vo.setDeptUser(dept);
			}
			}
			return vo;
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mjh.zhujianju.service.set.AdminService#getUserByUsername(java.lang.
	 * String)
	 */
	@Override
	public SysUser getUserByUsername(String username) {
		// TODO Auto-generated method stub
		return null;
	}

}
